package com.topsun.posclient.common.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.plugin.AbstractUIPlugin;

public class CacheImage {

	private final Map<String, Image> imageMap = new HashMap<String, Image>();
	private static CacheImage INSTANCE = new CacheImage();

	private CacheImage() {
	}

	public static CacheImage getINSTANCE() {
		if (null == INSTANCE) {
			return new CacheImage();
		} else {
			return INSTANCE;
		}
	}

	public Image getImage(String applicationID, String imageName) {
		if (imageName == null)
			return null;
		Image image = (Image) imageMap.get(imageName);
		if (image == null) {
			image = AbstractUIPlugin.imageDescriptorFromPlugin(applicationID, imageName).createImage();
			imageMap.put(imageName, image);// 图像加入到Map中 160. }
		}
		return image;
	}

	public void dispose() {
		// 将Map对象转化成Iterator对象 
		Iterator iterator = imageMap.values().iterator();
		while (iterator.hasNext())
			((Image) iterator.next()).dispose(); // 释放iterator中的图像资源 169.
													// imageMap.clear();
	}// 清除Map对象 170. }

}
